package com.oa.co.controller;


import com.github.pagehelper.PageInfo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.pojo.Performance;
import com.oa.co.pojo.User;
import com.oa.co.service.AuserService;
import com.oa.co.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/Auser")
public class AuserController {

    @Autowired
    private AuserService auserService;

    @RequestMapping("/auserList")
    public PageVo<User> performanceList(String username, String phone, Integer page, Integer limit){

        PageVo<User> pageVo = new PageVo<>();
        PageInfo<User> pageData =  auserService.selectUserList(page,limit,username,phone);
        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());
        pageVo.setData(pageData.getList());
        pageVo.setMsg("绩效分页数据");


        return pageVo;
    }

    //upload

    @ResponseBody
    @RequestMapping("/upload")
    public R upload(MultipartFile file, String username, HttpServletRequest request) throws IOException {
//        System.out.println(username);
//        System.out.println(file);

        String filename = file.getOriginalFilename();
        InputStream inputStream = file.getInputStream();

        String url = OssUtil.uploadFile(inputStream, filename);

        System.out.println("url "+url);

        return R.ok().data("url",url);
    }

    @RequestMapping("/auserAdd")
    public R auserAdd(@RequestBody User user){
        System.out.println("---=-  "+user);
        int num =  auserService.auserAdd(user);
        if (num ==  1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //delauser

    @RequestMapping("/delauser")
    public R delauser(Integer id){
        int num = auserService.delauser(id);
        if (num == 1){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    //editAuser

    @RequestMapping("/editAuser")
    public R editAuser(User user){
        int num  = auserService.editAuser(user);
        if (num ==  1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //delAllAuser
    @RequestMapping("/delAllAuser/{ids}")
    public R delAllAuser(@PathVariable("ids") String ids){
        String substring = ids.substring(1, ids.length() - 1);
        String[] split = substring.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s:split
        ) {
            int i = Integer.parseInt(s);
            list.add(i);
        }
        int num = auserService.delAllAuser(list);
        for (Integer i: list
        ) {
            System.out.println(i);
        }
        if(num != 0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
